<?php
require_once 'log/Logger.php';
$query_cnname = $_GET["query_cnname"];

Logger::getLogger()->debug('oddshistory enter');
//mId,host_cnname,visit_cnname,starttime,companyodds
if(!isset($query_cnname))
{
    Logger::getLogger()->debug('query_cnname is null');
    exit();
}
//Logger::getLogger()->debug('mysql_connect');
$conn = mysql_connect('ltfll.gicp.net','qqxj','qqxj');
if(!$conn)
{
    Logger::getLogger()->debug('connect fail!'.mysql_error());
    exit();
}
mysql_select_db('match',$conn);

mysql_query("SET NAMES 'GBK'");
//Logger::getLogger()->debug('memcache_connect');
$memcache_obj = memcache_connect("localhost", 11211);

$namekey = base64_encode($query_cnname);

//Logger::getLogger()->debug('base64_encode');
for($i = 0;$i < 15;$i++)
{
    $day = date("Ymd",time() - $i * 24 * 3600);
    
 //   $value = $memcache_obj->get("{$day}_matchdata_{$namekey}");
 //   if($value != null)
 //   {
 //       $oddsdata[] = $value;
 //   }
 //   else
    {
        $sql = "select id,mId,starttime,host_cnname,visit_cnname from {$day}_matchdata where host_cnname='{$query_cnname}' or visit_cnname='{$query_cnname}'";

        //Logger::getLogger()->debug($sql);
        $rs = mysql_query($sql, $conn);
        
        if(!$rs)
        {
            Logger::getLogger()->debug(mysql_error());
            continue;
        }
        
        if(mysql_num_rows($rs) == 0)
        {
            //Logger::getLogger()->debug(mysql_error());
            continue;
        }
        
        $row = mysql_fetch_array($rs);
        
        $oddsdata = array();
        while($row)
        {
   
            $mId = $row['mId'];
            $starttime = $row['starttime'];
            $companyodds = file_get_contents("oddsdata/{$day}oddsdata/{$mId}.odds");
            $matchdata = json_encode(array('id'=>$row['id'],'mId'=>$row['mId'],'starttime'=>$row['starttime'],'host_cnname'=>base64_encode($row['host_cnname']),'visit_cnname'=>base64_encode($row['visit_cnname']),'companyodds'=>$companyodds));
            $oddsdata[] = $matchdata;
            echo $matchdata;
            $md5 = md5($companyodds);
            $datalen = strlen($companyodds);
            Logger::getLogger()->debug("mId:{$mId},starttime:{$day},datalen:{$datalen},md5:{$md5}");
            $memcache_obj->set("{$day}_matchdata_{$namekey}", $matchdata);
                
            $row = mysql_fetch_array($rs);
        }
       	break; 
         
    }
    
}
$memcache_obj->close();
